import pandas as pd
from matplotlib import pyplot as plt

file_path = "02500005.csv"
# 读取原始数据
raw_data = pd.read_csv(file_path)

print(len(raw_data))

raw_data['data_time'] = pd.to_datetime(raw_data['data_time'], format='%Y-%m-%d %H:%M:%S')

# 提取月份和小时值，并创建新列 h 和 m
raw_data['h'] = raw_data['data_time'].dt.hour
raw_data['m'] = raw_data['data_time'].dt.month
raw_data["mm"] = raw_data['data_time'].dt.minute

mm_data = raw_data["mm"].values

data_time = raw_data['data_time'].values
ep_zone_data = raw_data['ep_zone'].values
p_data = raw_data['p'].values

import ipywidgets as widgets
from IPython.display import display, clear_output
# 获取时间范围
min_time = data_time.min()
max_time = data_time.max()

# 初始化时间滑动条
start_time_slider = widgets.SelectionSlider(
    options=data_time,
    value=min_time,
    description='Start Time:',
    continuous_update=False,
    layout=widgets.Layout(width='800px')  # 设置滑动条宽度为800像素
)

end_time_slider = widgets.SelectionSlider(
    options=data_time,
    value=max_time,
    description='End Time:',
    continuous_update=False,
    layout=widgets.Layout(width='800px')  # 设置滑动条宽度为800像素
)

# 更新图形的函数
def update_plot(start_time, end_time):
    # 清除之前的输出
    clear_output(wait=True)

    # 创建图形对象
    fig, ax = plt.subplots(figsize=(12, 6))

    # 获取时间范围内的索引
    start_index = raw_data[raw_data['data_time'] == start_time].index[0]
    end_index = raw_data[raw_data['data_time'] == end_time].index[0]

    # 绘制 ep_zone 的折线图
    ax.plot(data_time[start_index:end_index], ep_zone_data[start_index:end_index], label='ep_zone', marker='.', linestyle='-', color='blue')

    # 绘制 p_data 的折线图
    ax.plot(data_time[start_index:end_index], p_data[start_index:end_index], label='p_data', marker='x', linestyle='--', color='red')

    # 添加图例
    ax.legend()

    # 添加标题和坐标轴标签
    ax.set_title('ep_zone and p_data over Time')
    ax.set_xlabel('Time')
    ax.set_ylabel('Value')

    # 设置时间轴的格式
    fig.autofmt_xdate()  # 自动旋转日期标签，避免重叠
    ax.grid(True)  # 添加网格线

    # 限制 y 轴的最大值
    ax.set_ylim(bottom=0, top=1000)  # 设置 y 轴的范围，可以根据需要调整 top 的值

    # 显示图形
    plt.show()

# 将时间滑动条的值绑定到更新函数
widgets.interactive(update_plot, start_time=start_time_slider, end_time=end_time_slider)